package fucai.com.fucai.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import fucai.com.fucai.db.FuCaiNumber;
import fucai.com.fucai.db.SixThereCom;

/**
 * @说 明：这还少一个桥梁工具 如把txt转换为list集合
 * @项目名称：fucai
 * @包 名： fucai.com.fucai
 * @类 名： Fucai
 * @创 建人： chenshuangniu
 * @创建时间：2015/12/7 22:46
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */

public class LinkUtils {
    private static int oneline = 1; // 跳过第一行
    private static int tow = 1; // 跳过第一行

    public static boolean txtToList(List<FuCaiNumber> fuCaiNumber, File file) {
        FileReader fileReader = null;
        BufferedReader bReader = null;
        try {
            fileReader = new FileReader(file);
            bReader = new BufferedReader(fileReader);
            String cache = "";
            while ((cache = bReader.readLine()) != null) { // 注意保存
                if (oneline != 1) {
                    String str[] = cache.split("");
                 /*   muList.add(str[1] + str[2] + str[3] + str[4] + str[5] + ":"
                            + str[7] + "," + str[9] + "," + str[11] + ","
                            + str[13] + "," + str[15] + str[16] + "," + str[18]
                            + "," + str[20] + "," + str[22] + "," + str[24]);*/
                    FuCaiNumber fu = new FuCaiNumber();
                    fu.setIssue(str[1] + str[2] + str[3] + str[4] + str[5] + str[6] + str[7]);
                    int openBai = Integer.valueOf(str[9]);
                    int openShi = Integer.valueOf(str[11]);
                    int openGe = Integer.valueOf(str[13]);
                    fu.setOpenBaiWei(openBai);
                    fu.setOpenShiwei(openShi);
                    fu.setOpenGewei(openGe);
                    fu.setOpenType(str[15]);
                    fu.setOpenSum(openBai + openGe + openShi);
                    fu.setOpenSumEnd((openBai + openGe + openShi) % 10);
                    fu.setOpenSpan(Integer.valueOf(str[20]));
                    int testBai = Integer.valueOf(str[22]);
                    int testShi = Integer.valueOf(str[24]);
                    int testGe = Integer.valueOf(str[26]);
                    fu.setTestBaiWei(testBai);
                    fu.setTestShiwei(testShi);
                    fu.setTestGewei(testGe);
                    fu.setTestType(numberType(testBai, testShi, testGe));
                    fu.setTestSum(testBai + testShi + testGe);
                    fu.setTestSpan(numberSpan(testBai, testShi, testGe));
                    fu.setTestSumEnd((testBai + testShi + testGe) % 10);
                    fuCaiNumber.add(fu);
                }
                oneline = 2;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static boolean txtToListThereSix(List<SixThereCom> sixThere, File file) {
        FileReader fileReader = null;
        BufferedReader bReader = null;
        try {
            fileReader = new FileReader(file);
            bReader = new BufferedReader(fileReader);
            String cache = "";
            while ((cache = bReader.readLine()) != null) { // 注意保存
                if (tow != 1) {
                    SixThereCom fu = new SixThereCom();
                    String str[] = cache.split("");
                 /*   muList.add(str[1] + str[2] + str[3] + str[4] + str[5] + ":"
                            + str[7] + "," + str[9] + "," + str[11] + ","
                            + str[13] + "," + str[15] + str[16] + "," + str[18]
                            + "," + str[20] + "," + str[22] + "," + str[24]);*/

                    fu.setBaiwei(str[1]);
                    fu.setShiwei(str[2]);
                    fu.setGewei(str[3]);
                    sixThere.add(fu);
                }
                tow = 2;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 根据号码判断是否为组三 还还组六
     *
     * @param bai
     * @param shi
     * @param ge
     * @return
     */
    public static String numberType(int bai, int shi, int ge) {
        if (bai == shi || bai == ge || shi == ge) {
            return "三";
        } else {
            return "六";
        }
    }

    /**
     * 根据号码得到跨度
     *
     * @param bai
     * @param shi
     * @param ge
     * @return
     */
    public static int numberSpan(int bai, int shi, int ge) {
        int[] ss = {bai, shi, ge};
        Arrays.sort(ss);
        return Math.abs(ss[0] - ss[2]);
    }
}
